//
//  OSCEmoticonCollectionView.m
//  OSCEmoctionTools
//
//  Created by 8b410-2 on 17/1/6.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCEmoticonCollectionView.h"

@implementation OSCEmoticonCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self registerClass:[OSCEmoticonCollectionViewCell class] forCellWithReuseIdentifier:OSCEmoticonCollectionViewReuseId];
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return self;
}

@end

@interface OSCEmoticonCollectionViewCell ()

@property (nonatomic, strong) UIButton *emoticonView;

@end

@implementation OSCEmoticonCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _emoticonView = [UIButton new];
        [self addSubview:_emoticonView];
        _emoticonView.frame = CGRectInset(self.contentView.bounds, 4, 4);
        _emoticonView.backgroundColor = [UIColor clearColor];
        _emoticonView.userInteractionEnabled = NO;

    }
    return self;
}

- (void)setEmoticon:(OSCEmoticon *)emoticon {
    if (emoticon.chs != nil) {
        [_emoticonView setImage:[UIImage imageNamed:emoticon.imageName] forState:UIControlStateNormal];
    } else {
        [_emoticonView setImage:nil forState:UIControlStateNormal];
    }
    if (emoticon.emojiString) {
        [_emoticonView setTitle:emoticon.emojiString forState:UIControlStateNormal];
    }
    if (emoticon.isRemoveButton) {
        [_emoticonView setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    }
}

@end

@implementation OSCEmoticonLayout

- (void)prepareLayout {
    [super prepareLayout];
    
    CGFloat itemWidth = self.collectionView.bounds.size.width / emoticonMaxCols;
    CGFloat itemHeight = itemWidth;
    self.itemSize = CGSizeMake(itemWidth, itemHeight);
    
    CGFloat insetsY = (self.collectionView.bounds.size.height - itemHeight * emoticonMaxRows) * 0.5;
    self.collectionView.contentInset = UIEdgeInsetsMake(insetsY, 0, insetsY, 0);
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    
    self.minimumLineSpacing = 0;
    self.minimumInteritemSpacing = 0;
    
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
}

@end
